import tkinter
import cmath
import tkinter.messagebox


class Counter:
    def __init__(self):
        # 是否按下了运算符
        self.isopear = False
        self.lb = []
        self.root = tkinter.Tk()

        # 获取数字
        self.lb = []
        self.window()
        self.delete()
        self.ce()
        self.demo()
        self.empty()
        self.equal()
        self.pai()
        self.xiaoshu()
        self.yunsuan()
        self.zf()
        self.root.mainloop()

    def guanyu(self):
        tkinter.messagebox.showinfo('关于','计算器v1.2')
    #页面布局
    def window(self):
        #创建总菜单
        menu1=tkinter.Menu(self.root)
        #创建一个下拉菜单
        newmenu=tkinter.Menu(menu1)
        #创建下拉菜单的选项
        newmenu.add_command(label='关于',command=self.guanyu)
        #创建下拉菜单的分割线
        newmenu.add_separator()
        newmenu.add_command(label='退出',command=self.root.quit)
        menu1.add_cascade(label='File',menu=newmenu)
        self.root.config(menu=menu1)



        self.root.minsize(300, 455)
        self.root.title('计算器v1.2')
        self.root.iconbitmap(bitmap='./计算机.ico')
        self.root.resizable(False, False)
        # 显示区域
        self.v = tkinter.StringVar()
        self.v.set('0')
        self.s = tkinter.StringVar()
        self.s.set('')
        xianshi = tkinter.Label(textvariable=self.v, bg='pink', font=("黑体", 20, "bold"), justify=tkinter.LEFT, anchor='e')
        xianshi.place(x=10, y=40, width=280, height=80)
        xianshi2 = tkinter.Label(textvariable=self.s, bg='pink', font=("黑体", 10, "bold"), justify=tkinter.LEFT, anchor='e')
        xianshi2.place(x=10, y=10, width=280, height=40)

        # 按键1/x
        btn_mc = tkinter.Button(self.root, text='1/x', bg='white',command=self.fanbili)
        btn_mc.place(x=10, y=130, width=50, height=35)
        # 按键mR
        btn_mr = tkinter.Button(self.root, text='MR', bg='white')
        btn_mr.place(x=68, y=130, width=50, height=35)
        # 按键mS
        btn_ms = tkinter.Button(self.root, text='MS', bg='white')
        btn_ms.place(x=124, y=130, width=50, height=35)

        # 按键m+
        btn_mjia = tkinter.Button(self.root, text='M+', bg='white')
        btn_mjia.place(x=182, y=130, width=50, height=35)

        # 按键m-,
        btn_mjian = tkinter.Button(self.root, text='M-', bg='white')
        btn_mjian.place(x=240, y=130, width=50, height=35)

        # 按键删除键
        btn_tuige = tkinter.Button(self.root, text='←', bg='white', command=self.delete)
        btn_tuige.place(x=10, y=175, width=50, height=35)

        # 按键ce键
        btn_ce = tkinter.Button(self.root, text='CE', bg='white', command=self.ce)
        btn_ce.place(x=68, y=175, width=50, height=35)

        # 按键c键
        btn_c = tkinter.Button(self.root, text='C', bg='white', command=self.empty)
        btn_c.place(x=124, y=175, width=50, height=35)

        # 按键±键
        btn_zhengfu = tkinter.Button(self.root, text='±', bg='white', command=self.zf)
        btn_zhengfu.place(x=182, y=175, width=50, height=35)

        # 按键√
        btn_duihao = tkinter.Button(self.root, text='√', bg='white')
        btn_duihao.place(x=240, y=175, width=50, height=35)

        # 按键/
        btn_fanxiexian = tkinter.Button(self.root, text='/', bg='white', command=lambda:self. yunsuan('/'))
        btn_fanxiexian.place(x=182, y=220, width=50, height=35)

        # 按键%
        btn_bai = tkinter.Button(self.root, text='%', bg='white',command=lambda:self.yunsuan('%'))
        btn_bai.place(x=240, y=220, width=50, height=35)
        # 按键*
        btn_cheng = tkinter.Button(self.root, text='*', bg='white', command=lambda:self. yunsuan('*'))
        btn_cheng.place(x=182, y=265, width=50, height=35)
        # 按键π
        btn_pai = tkinter.Button(self.root, text='π', bg='white', command=self.pai)
        btn_pai.place(x=240, y=265, width=50, height=35)
        # 按键-
        btn_jian = tkinter.Button(self.root, text='-', bg='white', command=lambda: self.yunsuan('-'))
        btn_jian.place(x=182, y=310, width=50, height=35)
        # 按键=
        btn_deng = tkinter.Button(self.root, text='=', bg='white', command=self.equal)
        btn_deng.place(x=240, y=310, width=50, height=80)
        # 按键.
        btn_dian = tkinter.Button(self.root, text='.', bg='white', command=self.xiaoshu)
        btn_dian.place(x=124, y=355, width=50, height=35)
        # 按键+
        btn_jia = tkinter.Button(self.root, text='+', bg='white', command=lambda: self.yunsuan('+'))
        btn_jia.place(x=182, y=355, width=50, height=35)
        # 按键9
        btn_jiu = tkinter.Button(self.root, text='9', bg='white', command=lambda: self.demo('9'))
        btn_jiu.place(x=124, y=220, width=50, height=35)
        # 按键8
        btn_ba = tkinter.Button(self.root, text='8', bg='white', command=lambda:self. demo('8'))
        btn_ba.place(x=68, y=220, width=50, height=35)
        # 按键7
        btn_qi = tkinter.Button(self.root, text='7', bg='white', command=lambda: self.demo('7'))
        btn_qi.place(x=10, y=220, width=50, height=35)
        # 按键6
        btn_liu = tkinter.Button(self.root, text='6', bg='white', command=lambda: self.demo('6'))
        btn_liu.place(x=124, y=265, width=50, height=35)
        # 按键5
        btn_wu = tkinter.Button(self.root, text='5', bg='white', command=lambda: self.demo('5'))
        btn_wu.place(x=68, y=265, width=50, height=35)
        # 按键4
        btn_si = tkinter.Button(self.root, text='4', bg='white', command=lambda: self.demo('4'))
        btn_si.place(x=10, y=265, width=50, height=35)
        # 按键3
        btn_san = tkinter.Button(self.root, text='3', bg='white', command=lambda: self.demo('3'))
        btn_san.place(x=124, y=310, width=50, height=35)
        # 按键2
        btn_er = tkinter.Button(self.root, text='2', bg='white', command=lambda: self.demo('2'))
        btn_er.place(x=68, y=310, width=50, height=35)
        # 按键1
        btn_yi = tkinter.Button(self.root, text='1', bg='white', command=lambda: self.demo('1'))
        btn_yi.place(x=10, y=310, width=50, height=35)

        # 按键0
        btn_ling = tkinter.Button(self.root, text='0', bg='white', command=lambda: self.demo('0'))
        btn_ling.place(x=10, y=355, width=108, height=35)

        # 空白作者区
        btn_zz = tkinter.Button(self.root, text='   ', bg='pink')
        btn_zz.place(x=10, y=400, width=280, height=35)

        # 循环代码
        self.root.mainloop()


    #输入数字

    def demo(self,num):


        if self.isopear == False:
            if self.v.get() == '0':
                self.v.set('')
                self.v.set(num)


            else:
                self.v.set(self.v.get() + num)


        elif self.isopear==True:
            self.v.set(num)
            self.isopear = False



    # 小数点
    def xiaoshu(self):
        if '.' in self.v.get():
            return
        else:
            self.v.set(self.v.get() + '.')

        #print(self.v.get())

    # 运算
    def yunsuan(self,sign):

        self.isopear = True
        self.lb.append(self.v.get())
        print(self.lb)
        self.v.set('')

        if sign == '+':
            self.lb.append('+')
            a = (''.join(self.lb))
            self.s.set(a)

        if sign == '-':
            self.lb.append('-')
            a = (''.join(self.lb))
            self.s.set(a)

        if sign == '*':

            self.lb.append('*')
            a = (''.join(self.lb))
            self.s.set(a)

        if sign == '/':
            self.lb.append('/')
            a = (''.join(self.lb))
            self.s.set(a)

        if sign == '%':
            self.lb.append('%')
            a = (''.join(self.lb))
            self.s.set(a)
            # print(lb)


    # 等于
    def equal(self):
        # 获取当前页面的值准备运算
        self.lb.append(self.v.get())
        a = (''.join(self.lb))
        self.s.set(a)
        # print(lb)
        eqstr = ''.join(self.lb)
        # print(eqstr)
        if self.lb[-1] in '+-*/':
            eqstr = eqstr[:-1]
        # print(eqstr)
        # 运算并显示运算结果
        try:
            res=(str(eval(eqstr)))
            self.v.set(res[:18])
            # 清空列表
            self.lb.clear()
        except:
            self.v.set('除数不能为0')

    # 退格操作
    def delete(self):
        str = len(self.v.get())
        # print(v.get())
        if str > 1:
            str1 = self.v.get()
            self.v.set(str1[:-1])
        else:
            self.v.set('0')

    # 清空操作
    def empty(self):

        self.lb.clear()
        self.v.set('0')
        self.s.set('')
        # print(lb)

    # 正负号操作
    def zf(self):
        strnum = self.v.get()
        if strnum[0] == '-':
            self.v.set(strnum[1:])
        elif strnum[0] != '-' and strnum != '0':
            self.v.set('-' + strnum)

    # ce（）
    def ce(self):
        self.v.set('0')

    # pai
    def pai(self):
        self.v.set(cmath.pi)

    #1/x
    def  fanbili(self):
        self.lb.append(self.v.get())
        p='1'+'/'+''.join(self.lb)
        print(p,type(p))
        result=str(eval(p))
        self.v.set(result[:18])

#实例化对象
jsq=Counter()